package main

import (
	"flag"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/ifnk/micro-blog/internal/blog/controller"
	"github.com/ifnk/micro-blog/internal/blog/global"
	"github.com/ifnk/micro-blog/internal/comment"
	"github.com/ifnk/micro-blog/internal/dict"
	"github.com/ifnk/micro-blog/internal/permission"
	"github.com/ifnk/micro-blog/internal/pkg/config"
	"github.com/ifnk/micro-blog/internal/pkg/log"
	"github.com/ifnk/micro-blog/internal/post"
)

func main() {
	// 命令行可以 传 config.yaml 的路径 , 默认 的值的是  ./configs/config.yaml
	var flagConfig = flag.String("config", "./configs/config.yaml", "path to config file")
	logger := log.New()
	// 加载 配置文件 config.yaml 获取 config 实体
	conf, err := config.Load(*flagConfig)
	if err != nil {
		logger.Fatal(err)
	}
	global.Conf = conf

	// 初始化 client
	initClient(err, logger, conf)

	// 初始化路由
	r := initRouter()

	err = r.Run(conf.Blog.Server.HTTP.Port)
	if err != nil {
		logger.Infof("err -> %+v", err)
		logger.Error(fmt.Sprintf("服务启动失败 错误为 -> %+v", err))
	}
}

func initClient(err error, logger *log.Logger, conf *config.Config) {
	userClient, err := permission.NewUserClient(logger, conf)
	if err != nil {
		logger.Fatal(err)
	}
	userRltAuthClient, err := permission.NewUserRltAuthClient(logger, conf)
	if err != nil {
		logger.Fatal(err)
	}
	authorityClient, err := permission.NewAuthorityClient(logger, conf)
	if err != nil {
		logger.Fatal(err)
	}
	menuClient, err := permission.NewMenuClient(logger, conf)
	if err != nil {
		logger.Fatal(err)
	}
	apiClient, err := permission.NewApiClient(logger, conf)
	if err != nil {
		logger.Fatal(err)
	}
	casbinClient, err := permission.NewCasbinClient(logger, conf)
	if err != nil {
		logger.Fatal(err)
	}

	dictClient, err := dict.NewDictClient(logger, conf)
	if err != nil {
		logger.Fatal(err)
	}
	dictDetailClient, err := dict.NewDictDetailClient(logger, conf)
	if err != nil {
		logger.Fatal(err)
	}
	postClient, err := post.NewPostClient(logger, conf)
	if err != nil {
		logger.Fatal(err)
	}
	commentClient, err := comment.NewCommentClient(logger, conf)
	if err != nil {
		logger.Fatal(err)
	}

	global.UserClient = userClient
	global.UserRltAuthServiceClient = userRltAuthClient
	global.AuthorityClient = authorityClient
	global.CasbinClient = casbinClient
	global.MenuClient = menuClient
	global.ApiClient = apiClient

	global.DictClient = dictClient
	global.DictDetailClient = dictDetailClient

	global.PostClient = postClient
	global.CommentClient = commentClient

	global.Logger = logger
}

// 初始化路由配置
func initRouter() *gin.Engine {

	r := gin.New()

	r.Use(gin.Logger())   // 写日志 中间件
	r.Use(gin.Recovery()) // 全局异常处理 中间件

	// 给 一个 api 的路由组 (http://localhost:6666/api)
	api := r.Group("/api")
	controller.InitUserController(api)      // "/api/user"
	controller.InitAuthorityController(api) // "/api/authority"
	controller.InitMenuController(api)      // "/api/authority"
	controller.InitApiController(api)       // "/api/authority"(api)      // "/api/authority"
	controller.InitCasbinController(api)    // "/api/casbin"

	controller.InitDictController(api)
	controller.InitDictDetailController(api)

	controller.InitPostController(api)
	controller.InitCommentController(api)
	//controller.InitLoginController(api) // "/api/user"
	return r
}
